package stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream的三个操作步骤
 * 1、创建Stream
 * 2、中间操作
 * 3、终止操作（终端操作）
 */
public class StreamAPI {
    public static void main(String[] args) {
        //创建Stream
        //1、通过Collection系列集合提供的stream()或parallelStream()方法创建Stream
        List<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();

        //2、通过Arrays中的静态方法stream()获取
        Integer[] arr  = {1,2,3,4};
        Stream<Integer> stream1 = Arrays.stream(arr);

        //3、通过Stream类中的静态方法of()获取
        Stream<String> stream2 = Stream.of("aa", "bb", "cc", "dd");

        //4、创建无限流
        //迭代
        Stream<Integer> stream3 = Stream.iterate(0, (x) -> x + 2);
        //生成
        Stream<Double> stream4 = Stream.generate(Math::random);
        stream4.limit(5).forEach(System.out::println);

        //终止操作
        //stream3.forEach(System.out::println);
        //中间操作
        //stream3.limit(10).forEach(System.out::println);
    }
}
